package com.example.mybatisplusdemo.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.model.dto.PageDTO;
import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.service.IProductService;
import com.example.mybatisplusdemo.model.domain.Product;

import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 商品接口控制器
 *
 * @author lqj
 * @since 2025-06-24
 */
@RestController
@RequestMapping("/api/product")
public class ProductController {

    private final Logger logger = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private IProductService productService;

    /**
     * 根据 ID 查询商品
     */
    @GetMapping("/{id}")
    public JsonResponse<Product> getById(@PathVariable("id") Long id) {
        Product product = productService.getById(id);
        return JsonResponse.success(product);
    }

    /**
     * 分页查询商品
     */
    @GetMapping("pageList")
    public JsonResponse<Page<Product>> selectPageList(Product product, PageDTO pageDTO) {
        Page<Product> page = productService.pageList(product, pageDTO);
        return JsonResponse.success(page);
    }

    /**
     * 逻辑删除商品
     */
    @PostMapping("logicDelete/{id}")
    public JsonResponse logicDelete(@PathVariable("id") Long id) {
        int result = productService.logicDeleteById(id);
        return result > 0
                ? JsonResponse.success("删除成功")
                : JsonResponse.failure("删除失败，商品可能不存在或已被删除");
    }

    /**
     * 添加商品（含积分价格）
     */
    @PostMapping("addProduct")
    public JsonResponse addProduct(@RequestBody Product product) {
        boolean save = productService.save(product);
        return save
                ? JsonResponse.success("商品添加成功")
                : JsonResponse.failure("商品添加失败");
    }

    /**
     * 更新商品（包含积分价格字段）
     */
    @PutMapping("/update/{id}")
    public JsonResponse updateProduct(@PathVariable Long id, @RequestBody Product updateDto) {
        Product existing = productService.getById(id);
        if (existing == null) {
            return JsonResponse.failure("商品不存在");
        }

        // 更新字段
        if (updateDto.getName() != null) existing.setName(updateDto.getName());
        if (updateDto.getMainImage() != null) existing.setMainImage(updateDto.getMainImage());
        if (updateDto.getProductColor() != null) existing.setProductColor(updateDto.getProductColor());
        if (updateDto.getProductSize() != null) existing.setProductSize(updateDto.getProductSize());
        if (updateDto.getInventory() != null) existing.setInventory(updateDto.getInventory());
        if (updateDto.getPrice() != null) existing.setPrice(updateDto.getPrice());
        if (updateDto.getDiscountRate() != null) existing.setDiscountRate(updateDto.getDiscountRate());
        if (updateDto.getOrigin() != null) existing.setOrigin(updateDto.getOrigin());
        if (updateDto.getBrand() != null) existing.setBrand(updateDto.getBrand());
        if (updateDto.getPointsPrice() != null) existing.setPointsPrice(updateDto.getPointsPrice());

        existing.setGroupBuy(updateDto.getGroupBuy());
        existing.setPointsMall(updateDto.getPointsMall());

        productService.updateById(existing);

        return JsonResponse.success("商品更新成功");
    }

    /**
     * 排序分页查询
     */
    @PostMapping("/pageListWithSort")
    public JsonResponse selectPageListWithSort(@RequestBody PageDTO pageDTO) {
        Page<Product> page = productService.pageListWithSort(pageDTO);
        return JsonResponse.success(page);
    }

}
